package db

import (
	"apibe/single/src/v1/share/config"
	"github.com/go-redis/redis"
	"time"
)

// redis
const (
	CacheDB = iota
	CacheLog
	CacheWatch
)

var CacheDBClient *redis.Client // 操作登录缓存信息
var CacheWatchClient *redis.Client

// redisInit
// 初始化Redis
func redisInit() {
	// 初始化 CacheLogin
	CacheDBClient = redis.NewClient(&redis.Options{
		Addr:            config.Config.Redis.Url,
		Password:        config.Config.Redis.Password,
		DB:              CacheDB,
		MaxRetries:      1,
		MaxRetryBackoff: 10 * time.Second,
	})
	_, err := CacheDBClient.Ping().Result()
	if err != nil {
		panic("redis:  " + err.Error() + " url: " + config.Config.Redis.Url + " password:" + config.Config.Redis.Password)
	}
	// 初始化 CacheApi
	CacheWatchClient = redis.NewClient(&redis.Options{
		Addr:     config.Config.Redis.Url,
		Password: config.Config.Redis.Password,
		DB:       CacheWatch,
	})
	_, err = CacheWatchClient.Ping().Result()
	if err != nil {
		panic("redis:  " + err.Error() + " url: " + config.Config.Redis.Url + " password:" + config.Config.Redis.Password)
	}
	return
}
